﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace SMS.Magus
{
    public partial class frmAutoTel : DevExpress.XtraEditors.XtraUserControl
    {
        public frmAutoTel()
        {
            InitializeComponent();
        }
        //根据数值规则移动号码
        private void btnDownNumber_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyNum frm1 = new Magus.frmMovebyNum("down");
            frm1.ParentControl = this;

            //   frm1.Owner = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //根据数值规则移动号码
        private void btnUpNumber_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyNum frm1 = new Magus.frmMovebyNum("up");
            // frm1.Owner = this; frm1.Parent = this;
            frm1.ParentControl = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //自动生成号码
        private void btnInputNo_Click(object sender, EventArgs e)
        {
            //调用模态窗口，根据前7位自动生成号码
            Magus.frmInputNo frm1 = new Magus.frmInputNo();
            frm1.ParentControl = this;

            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //全部移动到下方
        private void btnDownAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem it in lvSourceNo.Items)
            {
                lvObjectNo.Items.Add(it.Text);
                lvSourceNo.Items.Remove(it);
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //全部移动到上方
        private void btnUpAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem it in lvObjectNo.Items)
            {
                lvSourceNo.Items.Add(it.Text);
                lvObjectNo.Items.Remove(it);
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //调用模态窗口，根据数值规则移动号码
        private void btnDownCombin_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyCombin frm1 = new Magus.frmMovebyCombin("down");
            frm1.ParentControl = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //根据数值规则移动号码
        private void btnUpCombin_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyCombin frm1 = new Magus.frmMovebyCombin("up");
            frm1.ParentControl = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //退出
        private void btnExit_Click(object sender, EventArgs e)
        {

        }
        //清空列表
        private void btnDelSource_Click(object sender, EventArgs e)
        {
            lvSourceNo.Items.Clear();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //清空重号
        private void btnDelObject_Click(object sender, EventArgs e)
        {
            lvObjectNo.Items.Clear();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //号码排序
        private void btnOrderObject_Click(object sender, EventArgs e)
        {
            lvObjectNo.Sorting = SortOrder.Ascending;
            lvObjectNo.Sort();
        }
        //号码乱序
        private void btnUnOrder_Click(object sender, EventArgs e)
        {
            lvObjectNo.Sorting = SortOrder.None;
            lvObjectNo.Sort();
        }
        //号码排序
        private void btnOrderSource_Click(object sender, EventArgs e)
        {
            lvSourceNo.Sorting = SortOrder.Ascending;
            lvSourceNo.Sort();
        }
        //打开文本文件
        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            //提示文件类型和格式
            Magus.frmFileMessage frm1 = new Magus.frmFileMessage();
            // frm1.ParentControl = this;
            frm1.ShowDialog();
            OpenFileDialog opendlg = new OpenFileDialog();
            opendlg.Title = "打开文本文件";
            opendlg.Filter = "文本文件 (*.txt)|*.txt";
            if (opendlg.ShowDialog() == DialogResult.OK)
            {
                ////遍历每个打开的文件名
                //foreach (string s in opendlg.FileNames)
                //{
                //    DevExpress.XtraEditors.XtraMessageBox.Show(s);
                //}
                string filename = opendlg.FileName;
                string strLine;
                try
                {
                    FileStream aFile = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(aFile);
                    strLine = sr.ReadLine();
                    while (strLine != null)
                    {
                        strLine.Trim();
                        lvSourceNo.Items.Add(strLine);
                        //读下一行
                        strLine = sr.ReadLine();
                    }
                    sr.Close();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString());
                    return;
                }
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //保存号码到文本文件
        private void btnSaveNo_Click(object sender, EventArgs e)
        {
            if (!(lvObjectNo.Items.Count > 0))
            {
                return;
            }
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Title = "保存号码到文本文件";
            saveDlg.Filter = "文本文件 (*.txt)|*.txt";
            if (saveDlg.ShowDialog() == DialogResult.OK)
            {
                string filename = saveDlg.FileName;
                string strLine;
                try
                {
                    FileStream aFile = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(aFile);
                    foreach (ListViewItem it in lvObjectNo.Items)
                    {
                        strLine = it.Text;
                        //读下一行
                        sw.WriteLine(strLine);
                    }
                    sw.Close();
                    lvObjectNo.Clear();
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString());
                    return;
                }
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //根据文本文件滤除号码
        private void btnFilter_Click(object sender, EventArgs e)
        {
            Magus.frmFileMessage frm1 = new Magus.frmFileMessage();
            // frm1.ParentControl = this;
            frm1.ShowDialog();
            OpenFileDialog opendlg = new OpenFileDialog();
            opendlg.Title = "根据文本文件滤除号码";
            opendlg.Filter = "文本文件 (*.txt)|*.txt";
            if (opendlg.ShowDialog() == DialogResult.OK)
            {
                string filename = opendlg.FileName;
                string strLine;
                string strAll = "";
                try
                {
                    FileStream aFile = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(aFile);
                    strLine = sr.ReadLine();
                    strAll += strLine + ",";
                    while (strLine != null)
                    {
                        strLine.Trim();
                        strAll += strLine + ",";
                        //读下一行
                        strLine = sr.ReadLine();
                    }
                    sr.Close();

                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString());
                    return;
                }
                foreach (ListViewItem it in lvObjectNo.Items)
                {
                    strLine = it.Text;
                    //读下一行
                    if (strAll.IndexOf(strLine) >= 0)
                    {
                        it.Remove();
                    }
                }
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //消除重号
        private void btnDelRepeat_Click(object sender, EventArgs e)
        {
            if (lvObjectNo.Items.Count < 2)
            {
                return;
            }
            //被比对的it首先定位第一个，从第2个开始循环与其比对
            ListViewItem it = lvObjectNo.Items[0];
            int k = 1;

            while (k < lvObjectNo.Items.Count)
            {
                for (int i = k; i < lvObjectNo.Items.Count; i++)
                {
                    //随着Remove操作，Count会变小，如果超出范围，打破循环
                    if (i >= lvObjectNo.Items.Count)
                        break;
                    if (lvObjectNo.Items[i].Text.Equals(it.Text))
                    {
                        lvObjectNo.Items[i].Remove();
                    }
                }
                //随着Remove操作，Count会变小，如果超出范围，无需再赋值
                //不超出范围，则被比对的it后移一位，开始比对的地方也后移1位
                if (k < lvObjectNo.Items.Count)
                    it = lvObjectNo.Items[k];
                k++;
            }
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //导入城市号段
        private void btnLoadNo_Click(object sender, EventArgs e)
        {
            //Magus.frmInputbyCity frm1 = new frmInputbyCity();
            //frm1.Owner = this;
            //frm1.ShowDialog();
            ////更新号码数量标签
            //setSourceCountlbl();
            //setObjectCountlbl();
        }
        //根据前面7位的范围规则移动
        private void btnUpArea_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyArea frm1 = new Magus.frmMovebyArea("up");
            frm1.ParentControl = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        //根据前面7位的范围规则移动
        private void btnDownArea_Click(object sender, EventArgs e)
        {
            Magus.frmMovebyArea frm1 = new Magus.frmMovebyArea("down");

            frm1.ParentControl = this;
            frm1.ShowDialog();
            //更新号码数量标签
            setSourceCountlbl();
            setObjectCountlbl();
        }
        public bool isnullorint(string str)
        {
            bool isint = true;
            if (str == "")
                str = "0";
            try
            {
                Int64 strint = Int64.Parse(str);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
            return isint;
        }

        private void setSourceCountlbl()
        {
            lblSourceCount.Text = "当前原始号码框中有" + lvSourceNo.Items.Count.ToString() + "个号码。";
        }
        private void setObjectCountlbl()
        {
            lblObjectCount.Text = "当前目标号码框中有" + lvObjectNo.Items.Count.ToString() + "个号码。";
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

    }
}
